package com.icetech.eom.controller.tx;


import com.github.pagehelper.PageInfo;
import com.icetech.commonbase.ResultTools;
import com.icetech.commonbase.constants.CodeConstantsEnum;
import com.icetech.commonbase.domain.Page;
import com.icetech.commonbase.domain.response.ObjectResponse;
import com.icetech.eom.common.annotation.SystemLog;
import com.icetech.eom.controller.BaseController;
import com.icetech.eom.controller.vo.ServerVo;
import com.icetech.eom.domain.sys.User;
import com.icetech.eom.domain.tx.Server;
import com.icetech.eom.service.tx.IServerService;
import com.icetech.eom.service.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务器 controller
 * </p>
 *
 * @author wangzw
 * @since 2018-11-12
 */
@Api(value = "Server-API",description = "这是服务器接口详细信息的描述")
@RestController
@RequestMapping("/server")
public class ServerController extends BaseController {

    @Autowired
    private IServerService serverService;
    @Autowired
    private  SecurityUtil securityUtil;
    /**
     * 获取服务列表
     */
    @ApiOperation(value = "获取服务器列表", notes = "根据查询信息获取服务器列表，返回List<User>类型用户信息的JSON")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchKey", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示条数", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/listpage",method = RequestMethod.GET)
    @SystemLog(description = "获取服务器列表")
    public ObjectResponse<Page> getServers(@RequestParam(value = "searchKey",defaultValue = "")String searchKey,
                                           @RequestParam(value = "page",defaultValue = "1")Integer page,
                                           @RequestParam(value = "pageSize",defaultValue = "20")Integer pageSize){
        //获取服务器列表信息
        PageInfo<Server> pageInfo = serverService.selectByPage(searchKey, page, pageSize);
        return ResultTools.getPageResponse(pageInfo.getList(),pageInfo.getTotal(),pageInfo.getPages());
    }

    /**
     * 增加服务器
     */
    @ApiOperation(value = "服务器添加", notes = "添加服务器信息")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @SystemLog(description = "服务器添加")
    public ObjectResponse add(@RequestBody ServerVo serverVo){
        User user = securityUtil.getCurrUser();
        Server server = new Server();
        BeanUtils.copyProperties(serverVo, server);
        server.setIsSystem(Boolean.FALSE);
        server.setCreator(user.getUserName());
        boolean isSuccess = serverService.save(server);
        return isSuccess?ResultTools.success(null):ResultTools.fail(CodeConstantsEnum.ERROR.getCode(),CodeConstantsEnum.ERROR.getDesc());
    }

    /**
     * 编辑服务器
     */
    @ApiOperation(value = "服务器编辑", notes = "编辑服务器信息")
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @SystemLog(description = "服务器编辑")
    public ObjectResponse edit(@RequestBody ServerVo serverVo){
        User user = securityUtil.getCurrUser();
        Server server = new Server();
        BeanUtils.copyProperties(serverVo, server);
        server.setModifier(user.getUserName());
        boolean isSuccess = serverService.updateById(server);
        return isSuccess?ResultTools.success(null):ResultTools.fail(CodeConstantsEnum.ERROR.getCode(),CodeConstantsEnum.ERROR.getDesc());
    }

    /**
     * 删除服务器
     */
    @ApiOperation(value = "服务器删除", notes = "删除服务器信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/remove",method = RequestMethod.GET)
    @SystemLog(description = "服务器删除")
    public ObjectResponse remove(@RequestParam("id")Long id){
        Server service = serverService.getById(id);
        if (Objects.isNull(service)){
            return ResultTools.fail(CodeConstantsEnum.ERROR_404.getCode(),CodeConstantsEnum.ERROR_404.getDesc());
        }
        boolean isSuccess = serverService.removeById(id);
        return isSuccess?ResultTools.success(null):ResultTools.fail(CodeConstantsEnum.ERROR.getCode(),CodeConstantsEnum.ERROR.getDesc());
    }
    /**
     * 删除服务器
     */
    @ApiOperation(value = "服务器批量删除", notes = "批量删除服务器信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id组成的数组", paramType = "query", dataType = "int")
    })
    @SystemLog(description = "服务器批量删除")
    @RequestMapping(value = "/batchremove",method = RequestMethod.GET)
    public ObjectResponse batchRemove(@RequestParam("ids") List<Long> ids){
        boolean isSuccess = serverService.removeByIds(ids);
        return isSuccess?ResultTools.success(null):ResultTools.fail(CodeConstantsEnum.ERROR.getCode(),CodeConstantsEnum.ERROR.getDesc());
    }

    /**
     * 导出操作
     */
    @ApiOperation(value = "服务器批量导出", notes = "批量导出服务器信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchKey", value = "关键字", paramType = "query", dataType = "String")
    })
    @SystemLog(description = "服务器批量导出")
    @RequestMapping(value = "/export",method = RequestMethod.GET)
    public ObjectResponse export(@RequestParam(value = "searchKey",defaultValue = "") String searchKey, HttpServletResponse response){
        try {
            Workbook workbook = serverService.exportExcel(searchKey);
            String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".xls";
            setFileResponse(response, fileName);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultTools.fail(CodeConstantsEnum.ERROR.getCode(),CodeConstantsEnum.ERROR.getDesc());
        }
        return ResultTools.success(null);
    }
}
